﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AD.BLL;
using AD.Model;

public partial class Manager_User_PageData_GuaranteeAudit : WebManagerForm
{
    public List<PayTradeMod> Pays = new List<PayTradeMod>();
    public int currPageIndex = 1;
    public int MaxSize = 0;
    public int MaxPage = 0;
    public string userName = "";
    public string startTime = "";
    public string endTime = "";
    public string email = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        string indexstr = Request.QueryString["pageIndex"];
        string action = Request.QueryString["action"];
        string idstrs = Request.Params["ids"];
        string idstr = Request.Params["id"];
        userName = Request.Params["userName"];
        startTime = Request.Params["startTime"];
        endTime = Request.Params["endTime"];
        email = Request.Params["email"];
        string filter = " RewardType=1 and IsInMoney=0 ";
        if (!string.IsNullOrEmpty(startTime))
            filter += string.Format(" and Commit_Date >= '%{0}%' ", startTime);
        if (!string.IsNullOrEmpty(endTime))
            filter += string.Format(" and Commit_Date<='{0}' ", endTime);

        if (!string.IsNullOrEmpty(action))
        {
            bool effect = int.TryParse(indexstr, out currPageIndex);
            if (action.Equals("queryFilter") && effect == true)
            {
                SetDataBind(email, filter, UtilHepler.PAGE_SIZE, 1);
            }
            else if (action.Equals("gopage") && effect == true)
            {
                SetDataBind(email, filter, UtilHepler.PAGE_SIZE, currPageIndex);
            }
            else if (action.Equals("delete") && effect == true)
            {
                PayTradeBLL.DeleteById(idstr);
                SetDataBind(email, filter, UtilHepler.PAGE_SIZE, currPageIndex);
            }
            else if (action.Equals("deleteAll") && !string.IsNullOrEmpty(idstrs))
            {
                List<int> ids = GetIdToList(idstrs);
                foreach (var id in ids)
                {
                    LogBLL.Delete(id);
                }
                SetDataBind(email, filter, UtilHepler.PAGE_SIZE, 1);
            }
            else if (action.Equals("inMoney") && !string.IsNullOrEmpty(idstrs))
            {
                PayTradeMod payTrade = PayTradeBLL.QueryByTradeNo(idstrs);
                if (payTrade != null)
                {
                    if (payTrade.IsInMoney == false)
                    {
                        PayTradeBLL.UpdateInMoneyStatus(idstrs);
                        payTrade = PayTradeBLL.QueryByTradeNo(idstrs);
                        if (payTrade != null && payTrade.IsInMoney == true && payTrade.Status == false)
                        {
                            if (PayTradeBLL.UpdatePaySuccessStatus(idstrs) > 0)
                            {
                                payTrade = PayTradeBLL.QueryByTradeNo(idstrs);
                                if (payTrade != null && payTrade.IsInMoney == true && payTrade.Status == true)
                                {
                                    UserMod user = UserBLL.QueryById(payTrade.UserId);
                                    if (user != null)
                                        //if (UtilHepler.UpdateAccountFlow(user, (Session[UtilHepler.SESSION_NAME] as ManagerUserMod).UserName, payTrade.Rechargepoints + payTrade.RewardPoints + payTrade.GiftPoints, OperationType.Invite) > 0)
                                        if (UtilHepler.UpdateAccountFlow(user, UtilHepler.GetMangerClientCookies(Request, Response).UserName, payTrade.Rechargepoints + payTrade.RewardPoints + payTrade.GiftPoints, OperationType.Invite) > 0)
                                            Alert("担保入金成功!");
                                }
                            }
                        }
                    }
                }
                SetDataBind(email, filter, UtilHepler.PAGE_SIZE, currPageIndex);
            }
        }
    }

    void SetDataBind(string name, string filter, int pageSize, int index)
    {
        if (!string.IsNullOrEmpty(name) || !string.IsNullOrEmpty(userName))
        {
            List<UserMod> users = null;
            string tmpFilter = "";
            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(userName))
            {
                tmpFilter = " Email='" + name + "' and Recommended='" + userName + "'";
            }
            else if (!string.IsNullOrEmpty(userName))
            {
                tmpFilter = " Recommended='" + userName + "'";
            }
            else if (!string.IsNullOrEmpty(name))
            {
                tmpFilter = " Email='" + name + "'";
            }
            users = UserBLL.QueryFilterToList(tmpFilter);
            if (users != null)
            {
                foreach (UserMod user in users)
                {
                    tmpFilter = filter + " and UserId=" + user.AutoID;
                    if (string.IsNullOrEmpty(filter))
                        tmpFilter = " UserId=" + user.AutoID;
                    List<PayTradeMod> tmp = PayTradeBLL.QueryByPage(tmpFilter, pageSize, index == 0 ? 0 : index - 1, out MaxSize);
                    if (tmp != null && tmp.Count > 0)
                    {
                        foreach (var pay in tmp)
                        {
                            pay.User = user;
                            pay.UserName = user.Email;
                        }
                        Pays.AddRange(tmp);
                    }
                }
            }
        }
        else
        {
            Pays = PayTradeBLL.QueryByPage(filter, pageSize, index == 0 ? 0 : index - 1, out MaxSize);
            if (Pays != null)
                foreach (var pay in Pays)
                {
                    UserMod user = UserBLL.QueryById(pay.UserId);
                    if (user != null)
                    {
                        pay.User = user;
                        pay.UserName = user.Email;
                    }
                }
            MaxPage = MaxSize / UtilHepler.PAGE_SIZE;
            if (MaxSize % UtilHepler.PAGE_SIZE != 0)
                MaxPage += 1;
            if (Pays == null) Pays = new List<PayTradeMod>();
        }
    }
}